package jp.co.nes.awssupport.control.function;

import java.time.YearMonth;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import jp.co.nes.awssupport.business.bean.CommonContainerBean;
import jp.co.nes.awssupport.business.bean.CommonContainerBean.ProcessingResult;
import jp.co.nes.awssupport.business.bean.CommonInfoBean;
import jp.co.nes.awssupport.business.bean.CommonInfoBean.RunningMode;
import jp.co.nes.awssupport.business.constant.OtherKeyForTargetYearMonth;
import jp.co.nes.awssupport.common.base.AbstractRequestHandler;
import jp.co.nes.awssupport.common.base.CommonException;
import jp.co.nes.awssupport.common.utility.BeanUtil;
import jp.co.nes.awssupport.common.utility.DateUtil;

/**
 * 処理対処年月作成です(Lambda)。
 */
public class TargetYearMonthCreator
        extends AbstractRequestHandler<CommonInfoBean, CommonContainerBean<CommonInfoBean>> {

    /**
     * デフォルトコンストラクタです。
     */
    public TargetYearMonthCreator() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.base.AbstractRequestHandler#handleRequest(java.lang.Object)
     */
    @Override
    protected CommonContainerBean<CommonInfoBean> handleRequest(CommonInfoBean input) {
        Map<String, String> other = input.getOther();
        if (other == null) {
            other = new HashMap<>();
        }
        RunningMode runningMode = input.getRunningMode();
        if (runningMode == RunningMode.AUTO_SAME_MONTH) {
            // 実行モードが自動実行(同月)の場合
            YearMonth sameYearMonth = DateUtil.getNowOfYearMonth();
            String targetYearMonthValue = DateUtil.toString(sameYearMonth);
            other.put(OtherKeyForTargetYearMonth.SAME.getMapKey(), targetYearMonthValue);
        } else if (runningMode == RunningMode.AUTO_SAME_MONTH_AND_LAST_MONTH) {
            // 実行モードが自動実行(同月と前月)の場合
            YearMonth sameYearMonth = DateUtil.getNowOfYearMonth();
            YearMonth lastYearMonth = sameYearMonth.plusMonths(-1);
            other.put(OtherKeyForTargetYearMonth.SAME.getMapKey(), DateUtil.toString(sameYearMonth));
            other.put(OtherKeyForTargetYearMonth.LAST.getMapKey(), DateUtil.toString(lastYearMonth));
        } else if (runningMode == RunningMode.AUTO_LAST_MONTH) {
            // 実行モードが自動実行(前月)の場合
            YearMonth sameYearMonth = DateUtil.getNowOfYearMonth();
            YearMonth lastYearMonth = sameYearMonth.plusMonths(-1);
            String targetYearMonthValue = DateUtil.toString(lastYearMonth);
            other.put(OtherKeyForTargetYearMonth.LAST.getMapKey(), targetYearMonthValue);
        } else if (runningMode == RunningMode.MANUAL) {
            // 実行モードが手動実行の場合、マップにInputをキーにして値が設定されている。
            other = input.getOther();
            if (other == null
                    || StringUtils.isEmpty(other.get(OtherKeyForTargetYearMonth.INPUT.getMapKey()))) {
                // 設定されていない場合はエラーとする。
                throw new CommonException("input parameter is wrong");
            }
        } else {
            throw new CommonException("RunningMode is wrong");
        }

        // 返却内容の設定
        CommonInfoBean info = new CommonInfoBean();
        BeanUtil.copyProperties(info, input);
        info.setOther(other);
        CommonContainerBean<CommonInfoBean> output = new CommonContainerBean<>();
        output.setInfoBean(info);
        output.setProcessingResult(ProcessingResult.SUCCESS);
        return output;
    }
}
